<?php
namespace app\Manage\controller;
use app\common\controller\Manage;
use app\common\model\Room as roomModel;
use app\common\model\Room;
use app\common\validate\Room as RoomValidate;
use app\common\model\Goods as GoodsModel;
use Request;


class Review extends Manage{
    /**
    * 商品列表
    * @return mixed
    */
    public function index()
    {
        $roomModel = new roomModel();
        if (Request::isAjax()) {
            $filter              = input('request.');
            return $roomModel->tableData($filter);
        }
        return $this->fetch('index');
    }

    public function add()
    {
        $goodsModel = new GoodsModel();
        $where['marketable'] = 1;
        $goodsList = $goodsModel->field('*')->where($where)->select();
        $this->assign('goodsList', $goodsList);
        $this->assign('roomClass', model('RoomClass')->all());
        return $this->fetch('add');
    }

    public function doAdd()
    {
        $result = [
            'status' => false,
            'msg'    => '',
            'data'   => '',
        ];
        //商品数据组装并校检
        $checkData = $this->checkRoomInfo();
        if (!$checkData['status']) {
            $result['msg'] = $checkData['msg'];
            return $result;
        }
        $data = $checkData['data'];
        //验证商品数据
        $roomModel    = new roomModel();
        $roomModel->startTrans();
        $room_id = $roomModel->doAdd($data['room']);

        if (!$room_id) {
            $roomModel->rollback();
            $result['msg'] = '房间数据保存失败';
            return $result;
        }
        $roomModel->commit();
        $result['msg']    = '保存成功';
        $result['status'] = true;
        return $result;
    }

    public function edit()
    {
        $room_id      = input("id");
        $roomModel    = new roomModel();
        $room         = $roomModel->getOne($room_id, '*');
        if (!$room['status']) {
            $this->error("无此商品");
        }
        $this->assign('data', $room['data']);
        $goodsModel = new GoodsModel();
        $where['marketable'] = 1;
        $goodsList = $goodsModel->field('*')->where($where)->select();
        $this->assign('goodsList', $goodsList);
        $this->assign('roomClass', model('RoomClass')->all());
        return $this->fetch('edit');
    }

    public function doEdit()
    {
        $result = [
            'status' => false,
            'msg'    => '',
            'data'   => '',
        ];
        //商品数据组装并校检
        $checkData = $this->checkRoomInfo(true);
        if (!$checkData['status']) {
            $result['msg'] = $checkData['msg'];
            return $result;
        }
        $data = $checkData['data'];
        //验证商品数据
        $roomModel    = new roomModel();
        $roomModel->startTrans();
        $updateRes = $roomModel->updateRoom($data['room']['id'], $data['room']);

        if (!$updateRes) {
            $roomModel->rollback();
            $result['msg'] = '房间数据保存失败';
            return $result;
        }
        $roomModel->commit();
        $result['msg']    = '保存成功';
        $result['status'] = true;
        return $result;
    }

    public function del()
    {
        $result = [
            'status' => false,
            'msg' => '删除失败',
            'data' => []
        ];
        $roomModel = new roomModel();
        if ($roomModel::destroy(input('param.id/d'), true)) {
            $result['status'] = true;
            $result['msg'] = '删除成功';
        }
        return $result;
    }

    private function checkRoomInfo($isEdit = false)
    {
        $result                         = [
            'status' => false,
            'msg'    => '',
            'data'   => '',
        ];
        $bn                             = get_sn(3);
        $data['room']['name']          = input('post.room.name', '');
        $data['room']['address']          = input('post.room.address', '');
        $data['room']['bn']            = input('post.room.bn', $bn);
        $data['room']['class_id']            = input('post.room.class_id', '');
        $data['room']['brief']         = input('post.room.brief', '');
        $data['room']['intro']         = input('post.room.intro', '');
        $data['room']['price']         = input('post.room.price', '');
        $data['room']['mktprice']      = input('post.room.mktprice', '');
        $data['room']['is_recommend']  = input('post.room.is_recommend', '2');
        $data['room']['sort']          = input('post.room.sort', '100');
        $images = input('post.room.img/a', []);
        if (count($images) <= 0) {
            $result['msg'] = '请先上传图片';
            return $result;
        }
        $data['room']['image_id']   = $images[0];
        $data['images']             = $images;
        array_shift($images);
        $data['room']['image_list'] = json_encode($images); 
        $data['room']['taocan'] = json_encode(input('post.taocan', ''),JSON_FORCE_OBJECT);

        if ($isEdit) {
            $data['room']['id'] = input('post.room.id/d', 0);
            $validate            = new RoomValidate();
            if (!$validate->scene('edit')->check($data['room'])) {
                $result['msg'] = $validate->getError();
                return $result;
            }
        } else {
            $validate = new RoomValidate();
            if (!$validate->check($data['room'])) {
                $result['msg'] = $validate->getError();
                return $result;
            }
        }
        $result['data']   = $data;
        $result['status'] = true;
        return $result;
    }

    public function getAll()
    {
        $result     = [
            'status' => false,
            'msg'    => '获取失败',
            'data'   => [],
        ];
        $roomModel = new roomModel();
        $typeList  = $roomModel->field('id, bn, taocan')->where([])->select();
        if (!$typeList->isEmpty()) {
            foreach ($typeList as $room) {
                $room->taocan = json_decode($room->taocan, true);
                if(!is_array($room->taocan)) {
                    $room->taocan = json_decode($room->taocan, true);
                }
            }
            
            $result['data']   = $typeList->toArray();
            $result['status'] = true;
            $result['msg']    = '获取成功';
        }
        return $result;
    }
}